#!/usr/bin/env python

#This is the main executable. Don't change it unless you really need to.

import sys
import re

DEFAULT_PORT = 1337

def print_usage():
   print
   print "Usage: ./client.py <remote IP> <options>"
   print "Remote IP can be written as <ip> or <ip>:<port>"
   print "Default port is " + str(DEFAULT_PORT)
   print
   print "If no <remote ip> is specified, the node will start in initial connection mode"
   print
   print "Options:"
   print "--help\tPrints this message."
   print "--about\tPrints credits and version information."
   print "--headless\tRuns node in headless mode without any UI."
   print "\t\tIgnores most options except remote IP and share directory."
   print "--port=<num>\tOverride the default port setting and choose your own."
   print



def print_about():
   print "\nUnnamed mystery filesharing project!!!1"
   print 'By Sam Greene, Matt McNeill, and Anthony Work\n'
   print "  /\___/\\"
   print " ( o   o )"
   print " (  =^=  )"
   print " (        )"
   print " (         )"
   print " (          )))))))))))"
   print "oh god how did this get here i am not good with computers\n"

def main():
   headless = 0
   connIP = ""
   connPort = -1
   myPort = DEFAULT_PORT

   shareDirs = []
   
   #Checkin' arguments, woo
   if len(sys.argv) > 1:
      regex = '^([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})(:([0-9])+)?'
      matched = 0
      for i in sys.argv:
         if i == "--about":
            print_about()
            return
         elif i == "--help":
            print_usage()
            return
         elif headless == 0 and i == "--headless":
            matched = 1
            headless = 1
         elif connIP == "" and re.match(regex, i):
            matched = 1
            ipStrings = i.split(":")
            connIP = ipStrings[0]
            if len(ipStrings) > 1:
               connPort = int(ipStrings[1])
         elif myPort == DEFAULT_PORT and re.match("^--port=[0-9]+", i):
            matched = 1
            myPort = int(i.split("=")[1])
         else:
            '''We can be sure that any leftover arguments are the directories that
            the user wants to share.'''
            matched = 1
            shareDirs.append(i)
      #This should never happen now! Remove later?
      if matched == 0:
         print_usage()
         return

   #Start the UI! DO IT
   if headless == 0:
      from ui_text import TextUI
      fileshare = TextUI(myPort, connIP, connPort, shareDirs)
   else:
      from ui_headless import HeadlessUI
      fileshare = HeadlessUI(myPort, connIP, connPort, shareDirs)


if __name__ == '__main__':
   main()

